Page({
  data: {
    currentType: 1, // 默认公司动态
    pageNum: 1,
    pageSize: 10,
    newsList: [],
    isLoading: true, // 初始加载状态
    hasMore: true,
    list: [], // 导航菜单数据
    carouselList: [
      { id: 1, imgUrl: "/image/buchou2.png", link: "/pages/index/index" },
      { id: 2, imgUrl: "/image/buchou1.png", link: "/pages/user/user" },
      { id: 3, imgUrl: "/image/buchou3.png", link: "" }     
    ],
    autoplay: true,
    interval: 3000,
    duration: 500,
    circular: true,
    showDots: true,
    dotColor: "#ffffff80",
    activeDotColor: "#ffffff",
    currentTab: 0
  },

  // 轮播图点击事件
  onCarouselTap(e) {
    const currentItem = this.data.carouselList[e.detail.current];
    if (currentItem.link) wx.navigateTo({ url: currentItem.link });
  },

  // 切换新闻类型
  switchType(e) {
    const type = Number(e.currentTarget.dataset.type);
    if (this.data.currentType === type) return;
    this.setData({ currentType: type, pageNum: 1, newsList: [], hasMore: true }, () => this.getNewsList());
  },

  // 请求新闻列表
  getNewsList() {
    if (this.data.isLoading || !this.data.hasMore) return;
    this.setData({ isLoading: true });
    wx.request({
      url: 'http://www.buchouwang.com/stage-api/website/getNewsList',
      method: 'GET',
      data: { pageNum: this.data.pageNum, pageSize: this.data.pageSize, newsType: this.data.currentType },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          const newList = res.data.rows || [];
          this.setData({
            newsList: this.data.pageNum === 1 ? newList : [...this.data.newsList, ...newList],
            pageNum: this.data.pageNum + 1,
            hasMore: newList.length === this.data.pageSize // 满页则有更多
          });
        } else wx.showToast({ title: '数据加载失败', icon: 'none' });
      },
      fail: () => wx.showToast({ title: '网络异常', icon: 'none' }),
      complete: () => this.setData({ isLoading: false })
    });
  },

  // 请求导航菜单数据
  getlist() {
    wx.request({
      url: 'http://www.buchouwang.com/stage-api/website/getProductMenuInfo',
      method: 'GET',
      success: (res) => {
        console.log(res);
        if (res.statusCode === 200 && res.data.code === 200) {
          this.setData({ list: res.data.data, isLoading: false });
        } else {
          wx.showToast({ title: '数据获取失败', icon: 'none' });
          this.setData({ isLoading: false });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络异常', icon: 'none' });
        this.setData({ isLoading: false });
      }
    });
  },

  // 导航菜单点击事件（预留）
  details(e) {
    const productId = e.currentTarget.dataset.id; // 获取产品ID
    if (!productId) return;
    wx.navigateTo({
      url: '/pages/details/details?id=' + productId // 跳转到产品详情页
    });
  },

  // 核心：新闻列表点击跳转详情页
  goNewsDetail(e) {
    const newsId = e.currentTarget.dataset.id; // 获取当前新闻的ID
    wx.navigateTo({
      url: '/pages/new-datails/new-datails?id=' + newsId // 跳转到详情页并携带ID
    });
  },

  // 生命周期
  onLoad() {},
  onShow() { this.getlist(); this.getNewsList(); },
  onReachBottom() { this.getNewsList(); },
  onPullDownRefresh() {
    this.setData({ pageNum: 1, newsList: [], hasMore: true }, () => {
      this.getNewsList().then(() => wx.stopPullDownRefresh());
    });
  }
});